<template>
  <div class="app-container">
    <div class="section-box">

      <el-row :gutter="10"
        class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning"
            plain
            icon="el-icon-close"
            size="mini"
            @click="handleClose">关闭</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading"
        :data="dataList">
        <el-table-column label="卦序"
          align="center"
          prop="guaId"
          min-width="60" />
        <el-table-column label="卦名"
          align="center"
          prop="guaName"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="关系"
          align="center"
          prop="relation"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="问题"
          align="center"
          prop="categoryName"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="创建人"
          align="center"
          prop="createBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="创建时间"
          align="center"
          prop="createTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人"
          align="center"
          prop="updateBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="更新时间"
          align="center"
          prop="updateTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="140">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">

        <el-form-item label="问题"
          prop="categoryId">
          <el-select v-model="form.categoryId"
            clearable
            placeholder="请选择问题"
            style="width: 100%">
            <el-option v-for="item in problemList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关系"
          prop="relation"
          style="width: 100%;">
          <el-input type="textarea"
            v-model="form.relation"
            style="width: 100%;"
            :rows="4"
            maxlength="2000"
            placeholder="请输入关系">
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getCategoryList,
  addCategory,
  updateCategory,
  delCategory
} from "@/api/divination/guaDivination.js";
import {
  getProblemList,
} from "@/api/resource/divinationProblem.js";
import { onInput } from '@/utils/util.js'

export default {
  name: "SayGua",
  dicts: ["yj_guai_level", 'yj_ji_xiong'],
  data () {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        guaId: null
      },
      // 数据列表
      dataList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      problemList: []
    };
  },
  created () {
    this.queryParams.guaId = this.$route.params && this.$route.params.id;
    this.getList();
    this.getProblemData()
  },
  methods: {
    onInput,
    getProblemData () {
      getProblemList({
        pageNum: 1,
        pageSize: 100,
      }).then(
        (response) => {
          this.problemList = response.rows;
        }
      );
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      getCategoryList(this.queryParams).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 返回按钮
    handleClose () {
      const obj = { path: '/gua/divination64' };
      this.$tab.closeOpenPage(obj);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        guaId: this.queryParams.guaId,
        categoryId: null,
        relation: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = '新增卦与占卜问题关系';
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = '修改卦与占卜问题关系';
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.yxId != undefined) {
            updateCategory(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addCategory(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const yxId = row.yxId;
      this.$modal
        // .confirm('是否确认删除Rate id编号为"' + rateIds + '"的数据项？')
        .confirm(`是否确认删除名嘴为 ${row.famousName} 的数据项？`)
        .then(function () {
          return delCategory(yxId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => { });
    }
  },
};
</script>
